import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;

import javax.swing.*;

@SuppressWarnings("serial")
public class MainFrame extends JFrame implements ActionListener {
	Vector<Film> filmList = new Vector<Film>();
	Vector<Music> musicList = new Vector<Music>();
	Vector<Review> reviewList = new Vector<Review>();
	JMenu Menu0 = new JMenu("文件");
	JMenuItem menuOpen = new JMenuItem("打开数据文件");
	JMenuItem menuSave = new JMenuItem("保存数据文件");
	JMenuItem menuExit = new JMenuItem("退出系统");
	JMenu Menu1 = new JMenu("信息维护");
	JMenuItem filmMenu = new JMenuItem("电影信息维护");
	JMenuItem musicMenu = new JMenuItem("电影原声带信息维护");
	JMenuItem reviewMenu = new JMenuItem("后期反响信息维护");

	public static void main(String[] args) {
		MainFrame f = new MainFrame();
		f.setVisible(true);
	}

	MainFrame() {
		super();
		JMenuBar menuBar = new JMenuBar();
		this.setJMenuBar(menuBar);
		filmMenu.addActionListener(this);
		filmMenu.setEnabled(false);
		Menu1.add(filmMenu);
		musicMenu.addActionListener(this);
		musicMenu.setEnabled(false);
		Menu1.add(musicMenu);
		reviewMenu.addActionListener(this);
		reviewMenu.setEnabled(false);
		Menu1.add(reviewMenu);
		menuOpen.addActionListener(this);
		Menu0.add(menuOpen);
		menuSave.addActionListener(this);
		menuSave.setEnabled(false);
		Menu0.add(menuSave);
		Menu0.addSeparator();
		menuExit.addActionListener(this);
		Menu0.add(menuExit);
		menuBar.add(Menu0);
		menuBar.add(Menu1);

		this.setTitle("电影资源管理系统");
		this.setResizable(false);
		this.setSize(700,320);
		this.setLocationRelativeTo(this.getOwner());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	void readFilmFile() {
		try {
			FileInputStream fis = new FileInputStream("Film.txt");
			InputStreamReader dis = new InputStreamReader(fis);
			BufferedReader reader = new BufferedReader(dis);
			String s;
			while ((s = reader.readLine()) != null) {
				Film film = new Film();
				String[] temp = s.split(" ");
				film.setFilmName(temp[0]);
				film.setFilmDirector(temp[1]);
				film.setFilmTime(temp[2]);
				film.setFilmActors(temp[3]);
				filmList.add(film);
			}
			reader.close();
			dis.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void writeFilmFile() {
		try {
			FileOutputStream fos = new FileOutputStream("Film.txt");
			OutputStreamWriter dos = new OutputStreamWriter(fos);
			BufferedWriter writer = new BufferedWriter(dos);
			for (int i = 0; i < filmList.size(); i++) {
				Film film = (Film) filmList.get(i);
				writer.write(film.getFilmName() + " " + film.getFilmDirector()
						+ " " + film.getFilmTime() + " " + film.getFilmActors()
						+ "\n");
			}
			writer.close();
			dos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void readMusicFile() {
		try {
			FileInputStream fis = new FileInputStream("Music.txt");
			InputStreamReader dis = new InputStreamReader(fis);
			BufferedReader reader = new BufferedReader(dis);
			String s;
			while ((s = reader.readLine()) != null) {
				Music music = new Music();
				String[] temp = s.split(" ");
				music.setMusicTheme(temp[0]);
				music.setMusicTime(temp[1]);
				music.setMusicMusician((String) temp[2]);
				music.setCriticism(temp[3]);
				musicList.add(music);
			}
			reader.close();
			dis.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void writeMusicFile() {
		try {
			FileOutputStream fos = new FileOutputStream("Music.txt");
			OutputStreamWriter dos = new OutputStreamWriter(fos);
			BufferedWriter writer = new BufferedWriter(dos);
			for (int i = 0; i < musicList.size(); i++) {
				Music music = (Music) musicList.get(i);
				writer.write(music.getMusicTheme() + " " + music.getMusicTime()
						+ " " + music.getMusicMusician() + " "
						+ music.getCriticism() + "\n");
			}
			writer.close();
			dos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void readReviewFile() {
		try {
			FileInputStream fis;
			fis = new FileInputStream("Review.txt");
			InputStreamReader dis = new InputStreamReader(fis);
			BufferedReader reader = new BufferedReader(dis);
			String s;
			while ((s = reader.readLine()) != null) {
				Review review = new Review();
				String[] temp = s.split(" ");
				review.setReviewAchievement(temp[0]);
				review.setReviewGrade(temp[1]);
				review.setCinecism(temp[2]);
				reviewList.add(review);
			}
			reader.close();
			dis.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void writeReciewFile() {
		try {
			FileOutputStream fos = new FileOutputStream("Reciew.txt");
			OutputStreamWriter dos = new OutputStreamWriter(fos);
			BufferedWriter writer = new BufferedWriter(dos);
			for (int i = 0; i < reviewList.size(); i++) {
				Review review = (Review) reviewList.get(i);
				writer.write(review.getReviewAchievement() + " "
						+ review.getReviewGrade() + " " + review.getCinecism()
						+ "\n");
			}
			writer.close();
			dos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void showFilmPanel() {
		FilmPanel filmPanel = new FilmPanel();
		filmPanel.filmList = this.filmList;
		this.add(filmPanel, BorderLayout.CENTER);
		filmPanel.showFilm(0);
		this.setVisible(true);
	}

	void showMusicPanel() {
		MusicPanel musicPanel = new MusicPanel();
		musicPanel.musicList = this.musicList;
		this.add(musicPanel, BorderLayout.CENTER);
		musicPanel.showMusic(0);
		this.setVisible(true);
	}

	void showReviewPanel() {
		ReviewPanel reviewPanel = new ReviewPanel();
		reviewPanel.reviewList = this.reviewList;
		this.add(reviewPanel, BorderLayout.CENTER);
		reviewPanel.showReview(0);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == filmMenu) {
			this.getContentPane().removeAll();
			this.showFilmPanel();
		}
		if (e.getSource() == musicMenu) {
			this.getContentPane().removeAll();
			this.showMusicPanel();
		}
		if (e.getSource() == reviewMenu) {
			this.getContentPane().removeAll();
			this.showReviewPanel();
		}
		if (e.getSource() == menuOpen) {
			this.filmList.removeAllElements();
			this.musicList.removeAllElements();
			this.reviewList.removeAllElements();
			this.readFilmFile();
			filmMenu.setEnabled(true);
			this.readMusicFile();
			musicMenu.setEnabled(true);
			this.readReviewFile();
			reviewMenu.setEnabled(true);
			menuSave.setEnabled(true);
			JOptionPane.showMessageDialog(null,
					"您已经成功打开数据：\n电影信息" + filmList.size() + "条\n电影原声带信息"
							+ musicList.size() + "条\n后期反响信息" + reviewList.size()
							+ "条", "打开", JOptionPane.INFORMATION_MESSAGE);
		}
		if (e.getSource() == menuSave) {
			this.writeFilmFile();
			this.writeMusicFile();
			this.writeReciewFile();
			menuSave.setEnabled(true);
			reviewMenu.setEnabled(true);
			JOptionPane.showMessageDialog(null,
					"您已经成功保存数据：\n电影信息" + filmList.size() + "条\n电影原声带信息"
							+ musicList.size() + "条\n后期反响信息" + reviewList.size()
							+ "条", "保存", JOptionPane.INFORMATION_MESSAGE);
		}
		if (e.getSource() == menuExit) {
			System.exit(0);
		}
	}

}

@SuppressWarnings("serial")
class ReviewPanel extends JPanel implements ActionListener {
	private JTextField filmNo = new JTextField(); // 影片信息
	private JTextField musicNo = new JTextField(); // 电影原声带
	private JTextField Review = new JTextField(); // 后期反响
	Vector<Review> reviewList = new Vector<Review>();
	int count = 0, current = 0, inserting = 0;

	private String[] btnStr = { "第一个", "上一个", "下一个", "最后一个", "添加", "修改", "删除" };
	private JButton[] btn = new JButton[btnStr.length];

	ReviewPanel() {
		this.setLayout(null);
		// 显示票房
		JLabel lb1 = new JLabel("票房：");
		lb1.setBounds(30, 10, 100, 20);
		this.add(lb1);
		filmNo.setBounds(100, 10, 100, 20);
		this.add(filmNo);
		// 显示评分
		JLabel lb2 = new JLabel("评分：");
		lb2.setBounds(30, 60, 100, 20);
		this.add(lb2);
		musicNo.setBounds(100, 60, 100, 20);
		this.add(musicNo);
		// 显示影评
		JLabel lb3 = new JLabel("影评：");
		lb3.setBounds(30, 110, 100, 20);
		this.add(lb3);
		Review.setBounds(100, 110, 100, 20);
		this.add(Review);

		for (int i = 0; i < btn.length; i++) {
			btn[i] = new JButton(btnStr[i]);
			btn[i].setBounds(30 + i * 90, 210, 90, 30);
			btn[i].addActionListener(this);
			this.add(btn[i]);
		}

	}

	void showReview(int offset) {
		Review review = (Review) reviewList.get(offset);
		this.filmNo.setText(review.getReviewAchievement());
		this.musicNo.setText("" + review.getReviewGrade());
		this.Review.setText("" + review.getCinecism());
	}

	public void actionPerformed(ActionEvent e) {
		count = this.reviewList.size();
		if (e.getSource() == this.btn[0]) {
			this.showReview(0);
			current = 0;
		}
		if (e.getSource() == this.btn[1] && current > 0) {
			this.showReview(current - 1);
			current = current - 1;
		}
		if (e.getSource() == this.btn[2] && current < count - 1) {
			this.showReview(current + 1);
			current = current + 1;
		}
		if (e.getSource() == this.btn[3]) {
			this.showReview(count - 1);
			current = count - 1;
		}
		if (e.getSource() == this.btn[4]) {
			if (this.inserting == 0) {
				this.filmNo.setText("");
				this.musicNo.setText("");
				this.Review.setText("");
				btn[4].setText("保存");
				btn[5].setText("取消");
				this.inserting = 1;
			} else {
				Review review = new Review();
				review.setReviewAchievement(this.filmNo.getText().trim());
				review.setReviewGrade(this.musicNo.getText().trim());
				review.setCinecism(this.Review.getText().trim());
				reviewList.add(review);
				count++;
				current = count - 1;
				btn[4].setText("添加");
				this.inserting = 0;
			}
			for (int i = 0; i < btn.length; i++) {
				if (i == 4 || i == 5)
					continue;
				btn[i].setEnabled(!btn[i].isEnabled());
			}
		}
		if (e.getSource() == this.btn[5]) {
			if (this.inserting == 0) {
				Review review = (Review) reviewList.get(current);
				review.getReviewAchievement();
				review.getReviewGrade();
				review.getCinecism();
			} else {
				btn[4].setText("添加");
				btn[5].setText("修改");
				for (int i = 0; i < btn.length; i++) {
					if (i == 4 || i == 5)
						continue;
					btn[i].setEnabled(!btn[i].isEnabled());
				}
				this.inserting = 0;
				this.showReview(current);
			}
		}
		if (e.getSource() == this.btn[6]) {
			if (count == 0)
				return;
			reviewList.remove(current);
			count--;
			if (count == 0) {
				this.filmNo.setText("");
				this.musicNo.setText("");
				this.Review.setText("");
			} else {
				if (current > count - 1) {
					this.showReview(current - 1);
					current = current - 1;
				} else
					this.showReview(current);
			}
		}
		this.repaint();
	}
}

@SuppressWarnings("serial")
class FilmPanel extends JPanel implements ActionListener {
	private JTextField filmName = new JTextField(); // 电影名称
	private JTextField filmDirector = new JTextField(); // 导演
	private JTextField filmTime = new JTextField(); // 时间
	private JTextField filmActors = new JTextField(); // 演员
	Vector<Film> filmList = new Vector<Film>();
	int count = 0, current = 0, inserting = 0;

	private String[] btnStr = { "第一个", "上一个", "下一个", "最后一个", "添加", "修改", "删除" };
	private JButton[] btn = new JButton[btnStr.length];

	FilmPanel() {
		this.setLayout(null);
		// 显示电影名称
		JLabel lb1 = new JLabel("电影名称：");
		lb1.setBounds(30, 10, 100, 20);
		this.add(lb1);
		filmName.setBounds(100, 10, 100, 20);
		this.add(filmName);
		// 显示导演
		JLabel lb2 = new JLabel("导演姓名：");
		lb2.setBounds(30, 60, 100, 20);
		this.add(lb2);
		filmDirector.setBounds(100, 60, 100, 20);
		this.add(filmDirector);
		// 显示时间
		JLabel lb3 = new JLabel("上映时间：");
		lb3.setBounds(30, 110, 100, 20);
		this.add(lb3);
		filmTime.setBounds(100, 110, 100, 20);
		this.add(filmTime);
		// 显示演员
		JLabel lb4 = new JLabel("演员信息：");
		lb4.setBounds(30, 160, 100, 20);
		this.add(lb4);
		filmActors.setBounds(100, 160, 100, 20);
		this.add(filmActors);

		for (int i = 0; i < btn.length; i++) {
			btn[i] = new JButton(btnStr[i]);
			btn[i].setBounds(30 + i * 90, 210, 90, 30);
			btn[i].addActionListener(this);
			this.add(btn[i]);
		}
	}

	void showFilm(int offset) {
		Film film = (Film) filmList.get(offset);
		this.filmName.setText(film.getFilmName());
		this.filmDirector.setText(film.getFilmDirector());
		this.filmTime.setText(film.getFilmTime());
		this.filmActors.setText(film.getFilmActors());
	}

	public void actionPerformed(ActionEvent e) {
		count = this.filmList.size();
		if (e.getSource() == this.btn[0]) {
			this.showFilm(0);
			current = 0;
		}
		if (e.getSource() == this.btn[1] && current > 0) {
			this.showFilm(current - 1);
			current = current - 1;
		}
		if (e.getSource() == this.btn[2] && current < count - 1) {
			this.showFilm(current + 1);
			current = current + 1;
		}
		if (e.getSource() == this.btn[3]) {
			this.showFilm(count - 1);
			current = count - 1;
		}
		if (e.getSource() == this.btn[4]) {
			if (this.inserting == 0) {
				this.filmName.setText("");
				this.filmDirector.setText("");
				this.filmTime.setText("");
				this.filmActors.setText("");
				btn[4].setText("保存");
				btn[5].setText("取消");
				this.inserting = 1;
			} else {
				Film film = new Film();
				film.setFilmName(this.filmName.getText().trim());
				film.setFilmDirector(this.filmDirector.getText().trim());
				film.setFilmTime(this.filmTime.getText().trim());
				film.setFilmActors(this.filmActors.getText().trim());
				filmList.add(film);
				count++;
				current = count - 1;
				btn[4].setText("添加");
				this.inserting = 0;
			}
			for (int i = 0; i < btn.length; i++) {
				if (i == 4 || i == 5)
					continue;
				btn[i].setEnabled(!btn[i].isEnabled());
			}
		}

		if (e.getSource() == this.btn[5]) {
			if (this.inserting == 0) {
				Film film = (Film) filmList.get(current);
				film.setFilmName(this.filmName.getText().trim());
				film.setFilmDirector(this.filmDirector.getText().trim());
				film.setFilmTime(this.filmTime.getText().trim());
				film.setFilmActors(this.filmActors.getText().trim());
			} else {
				btn[4].setText("添加");
				btn[5].setText("修改");
				for (int i = 0; i < btn.length; i++) {
					if (i == 4 || i == 5)
						continue;
					btn[i].setEnabled(!btn[i].isEnabled());
				}
				this.inserting = 0;
				this.showFilm(current);
			}
		}
		if (e.getSource() == this.btn[6]) {
			if (count == 0)
				return;
			filmList.remove(current);
			count--;
			if (count == 0) {
				this.filmName.setText("");
				this.filmDirector.setText("");
				this.filmTime.setText("");
				this.filmActors.setText("");
			} else {
				if (current > count - 1) {
					this.showFilm(current - 1);
					current = current - 1;
				} else
					this.showFilm(current);
			}
		}
		this.repaint();
	}
}

@SuppressWarnings("serial")
class MusicPanel extends JPanel implements ActionListener {
	private JTextField musicTheme = new JTextField(); // 主题曲
	private JTextField musicTime = new JTextField(); // 时长
	private JTextField musicMusician = new JTextField(); // 歌曲
	private JTextField criticism = new JTextField();//乐评

	Vector<Music> musicList = new Vector<Music>();
	int count = 0, current = 0, inserting = 0;

	private String[] btnStr = { "第一个", "上一个", "下一个", "最后一个", "添加", "修改", "删除" };
	private JButton[] btn = new JButton[btnStr.length];

	MusicPanel() {
		this.setLayout(null);
		// 显示主题曲
		JLabel lb1 = new JLabel("主题曲：");
		lb1.setBounds(30, 10, 100, 20);
		this.add(lb1);
		musicTheme.setBounds(100, 10, 100, 20);
		this.add(musicTheme);
		// 显示时长
		JLabel lb2 = new JLabel("时长：");
		lb2.setBounds(30, 60, 100, 20);
		this.add(lb2);
		musicTime.setBounds(100, 60, 100, 20);
		this.add(musicTime);
		// 显示歌手
		JLabel lb3 = new JLabel("歌手：");
		lb3.setBounds(30, 110, 100, 20);
		this.add(lb3);
		musicMusician.setBounds(100, 110, 100, 20);
		this.add(musicMusician);
		// 显示乐评
		JLabel lb4 = new JLabel("乐评：");
		lb4.setBounds(30, 160, 100, 20);
		this.add(lb4);
		criticism.setBounds(100, 160, 100, 20);
		this.add(criticism);

		for (int i = 0; i < btn.length; i++) {
			btn[i] = new JButton(btnStr[i]);
			btn[i].setBounds(30 + i * 90, 210, 90, 30);
			btn[i].addActionListener(this);
			this.add(btn[i]);
		}
	}

	void showMusic(int offset) {
		Music music = (Music) musicList.get(offset);
		this.musicTheme.setText(music.getMusicTheme());
		this.musicTime.setText(music.getMusicTime());
		this.musicMusician.setText("" + music.getMusicMusician());
		this.criticism.setText(music.getCriticism());
	}

	public void actionPerformed(ActionEvent e) {
		count = this.musicList.size();
		if (e.getSource() == this.btn[0]) {
			this.showMusic(0);
			current = 0;
		}
		if (e.getSource() == this.btn[1] && current > 0) {
			this.showMusic(current - 1);
			current = current - 1;
		}
		if (e.getSource() == this.btn[2] && current < count - 1) {
			this.showMusic(current + 1);
			current = current + 1;
		}
		if (e.getSource() == this.btn[3]) {
			this.showMusic(count - 1);
			current = count - 1;
		}
		if (e.getSource() == this.btn[4]) {
			if (this.inserting == 0) {
				this.musicTheme.setText("");
				this.musicTime.setText("");
				this.musicMusician.setText("");
				this.criticism.setText("");
				btn[4].setText("保存");
				btn[5].setText("取消");
				this.inserting = 1;
			} else {
				Music music = new Music();
				music.setMusicTheme(this.musicTheme.getText().trim());
				music.setMusicTime(this.musicTime.getText().trim());
				music.setMusicMusician((this.musicMusician.getText().trim()));
				music.setCriticism(this.criticism.getText().trim());
				musicList.add(music);
				count++;
				current = count - 1;
				btn[4].setText("添加");
				this.inserting = 0;
			}
			for (int i = 0; i < btn.length; i++) {
				if (i == 4 || i == 5)
					continue;
				btn[i].setEnabled(!btn[i].isEnabled());
			}
		}

		if (e.getSource() == this.btn[5]) {
			if (this.inserting == 0) {
				Music music = (Music) musicList.get(current);
				music.setMusicTheme(this.musicTheme.getText().trim());
				music.setMusicTime(this.musicTime.getText().trim());
				music.setMusicMusician((this.musicMusician.getText().trim()));
				music.setCriticism(this.criticism.getText().trim());
			} else {
				btn[4].setText("添加");
				btn[5].setText("修改");
				for (int i = 0; i < btn.length; i++) {
					if (i == 4 || i == 5)
						continue;
					btn[i].setEnabled(!btn[i].isEnabled());
				}
				this.inserting = 0;
				this.showMusic(current);
			}
		}
		if (e.getSource() == this.btn[6]) {
			if (count == 0)
				return;
			musicList.remove(current);
			count--;
			if (count == 0) {
				this.musicTheme.setText("");
				this.musicTime.setText("");
				this.musicMusician.setText("");
				this.criticism.setText("");
			} else {
				if (current > count - 1) {
					this.showMusic(current - 1);
					current = current - 1;
				} else
					this.showMusic(current);
			}
		}
		this.repaint();
	}
}

class Film {
	private String filmName; // 影片名
	private String filmDirector; // 导演
	private String filmTime; // 时间
	private String filmActors; // 演员
	Review review; // 票房

	public String getFilmName() {
		return filmName;
	}

	public void setFilmName(String filmName) {
		this.filmName = filmName;
	}

	public String getFilmDirector() {
		return filmDirector;
	}

	public void setFilmDirector(String filmDirector) {
		this.filmDirector = filmDirector;
	}

	public String getFilmTime() {
		return filmTime;
	}

	public void setFilmTime(String filmTime) {
		this.filmTime = filmTime;
	}

	public String getFilmActors() {
		return filmActors;
	}

	public void setFilmActors(String filmActors) {
		this.filmActors = filmActors;
	}
}

class Music {
	private String musicTheme; // 主题曲
	private String musicTime; // 时长
	private String musicMusician; // 歌手
	private String criticism; // 乐评

	public String getMusicTheme() {
		return musicTheme;
	}

	public void setMusicTheme(String musicTheme) {
		this.musicTheme = musicTheme;
	}

	public String getMusicTime() {
		return musicTime;
	}

	public void setMusicTime(String musicTime) {
		this.musicTime = musicTime;
	}

	public String getMusicMusician() {
		return musicMusician;
	}

	public void setMusicMusician(String musicMusician) {
		this.musicMusician = musicMusician;
	}

	public String getCriticism() {
		return criticism;
	}

	public void setCriticism(String criticism) {
		this.criticism = criticism;
	}
}

class Review {
	private String reviewAchievement; // 票房
	private String reviewGrade; // 评分
	private String cinecism; // 影评

	public String getReviewAchievement() {
		return reviewAchievement;
	}

	public void setReviewAchievement(String reviewAchievement) {
		this.reviewAchievement = reviewAchievement;
	}

	public String getReviewGrade() {
		return reviewGrade;
	}

	public void setReviewGrade(String reviewGrade) {
		this.reviewGrade = reviewGrade;
	}

	public String getCinecism() {
		return cinecism;
	}

	public void setCinecism(String review) {
		this.cinecism = review;
	}
}

